package com.liuh.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liuh.pojo.ClassesInfo;
import com.liuh.pojo.StuInfo;
import com.liuh.service.ClassService;
import com.liuh.service.StudentService;
import com.liuh.service.impl.ClassServiceImpl;
import com.liuh.service.impl.StudentServiceImpl;
import com.liuh.util.OutMsg;
import com.liuh.util.Page;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.net.multipart.MultipartFormData;
import cn.hutool.core.net.multipart.UploadFile;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * @note 学员操作servlet
 * @author liuh
 * @date 2021年4月27日 上午9:21:07
 */
@SuppressWarnings("serial")
public class StudentServlet extends BaseServlet {

	StudentService studentS = new StudentServiceImpl();
	ClassService clas = new ClassServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
		//获取页面参数值
		Map<String, String> map1 = ServletUtil.getParamMap(req);
		String opr = map1.get("opr");
		if ("list".equals(opr)) {
			// 根据查询条件获取分页对象
			Page<StuInfo> page = studentS.findPageStu(map1);
			// 班级信息
			List<ClassesInfo> clas_list = clas.findClasses();
			// 响应结果setAttribute
			req.setAttribute("page", page);
			req.setAttribute("cla_list", clas_list);
			req.setAttribute("map", map1);
			req.getRequestDispatcher("stu/stu-list.jsp").forward(req, resp);
		} else if ("cla_list_ajax".equals(opr)) {
			resp.setContentType("application/json;charset=UTF-8");
			// 班级信息
			List<ClassesInfo> clas_list = clas.findClasses();
			Dict msg = OutMsg.success("成功", clas_list);
			outReps(JSONUtil.toJsonPrettyStr(msg), resp);
		} else if ("list_ajax".equals(opr)) {
			resp.setContentType("application/json;charset=UTF-8");
			// 根据查询条件获取分页对象
			Page<StuInfo> page = studentS.findPageStu(map1);
			// 构造响应json对象
			Dict msg = OutMsg.success("成功").set("data", page);
			JSONObject json = JSONUtil.parseObj(msg).setDateFormat("yyyy-MM-dd HH:mm:ss");
			outReps(json.toStringPretty(), resp);
		} else if ("saveOrUpdate".equals(opr)) {
			resp.setContentType("application/json;charset=UTF-8");
			StuInfo stuInfo2 = ServletUtil.toBean(req, StuInfo.class, true);
			//String[] like1 = req.getParameter("like1").split(",");
			// 保存或者更新学生信息
			int result = studentS.saveOrUpdate(stuInfo2);
			// 构造响应json对象
			Dict msg = result > 0 ? OutMsg.success("成功") : OutMsg.fail("失败");
			outReps(JSONUtil.toJsonStr(msg), resp);
		} else if ("del".equals(opr)) {
			resp.setContentType("application/json;charset=UTF-8");
			// 删除学员信息
			int result = studentS.delStu(Convert.toInt(map1.get("id")));
			// 构造响应json对象
			Dict msg = result > 0 ? OutMsg.success("成功") : OutMsg.fail("失败");
			outReps(JSONUtil.toJsonStr(msg), resp);
		} else if ("delAll".equals(opr)) {
			resp.setContentType("application/json;charset=UTF-8");
			Object[] ids = Convert.convert(Object[].class, StrUtil.splitTrim(map1.get("ids"), ","));
			// 批量删除学员信息
			int result = studentS.delAllStu(ids);
			// 构造响应json对象
			Dict msg = result > 0 ? OutMsg.success("成功") : OutMsg.fail("失败");
			outReps(JSONUtil.toJsonStr(msg), resp);
		} else if ("editView".equals(opr)) {
			// 根据id查找学员对象
			StuInfo stuInfo = studentS.findStuById(Convert.toInt(map1.get("id"), -1));
			// 班级信息
			List<ClassesInfo> clas_list = clas.findClasses();
			req.setAttribute("cla_list", clas_list);
			req.setAttribute("stu", stuInfo);
			if (stuInfo != null) {
				// 格式化日期
				req.setAttribute("birthday", DateUtil.format(stuInfo.getStuBirthDay(), "yyyy-MM-dd HH:mm:ss"));
			}
			req.getRequestDispatcher("stu/stu-add.jsp").forward(req, resp);
		} else if ("doUpload".equals(opr)) {
			resp.setContentType("application/json;charset=UTF-8");
			String uploadFilePath = req.getSession().getServletContext().getRealPath("upload/" );
			MultipartFormData formData=ServletUtil.getMultipart(req);
			String name=formData.getParam("uName");
			System.out.println(name);
			UploadFile uploadFile=formData.getFile("file");
			File targetFile=new File(uploadFilePath+uploadFile.getFileName());
			uploadFile.write(targetFile);
			Dict msg =OutMsg.success("成功").set("data", targetFile.getAbsolutePath());
			outReps(JSONUtil.toJsonStr(msg), resp);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}

}
